import requests
import json
import os
from datetime import datetime
from dotenv import load_dotenv

# 加载配置
load_dotenv('.env')

# 配置信息
GITHUB_USERNAME = os.getenv('GITHUB_USERNAME')
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
STAR_FILE = os.getenv('STAR_FILE')
PER_PAGE = os.getenv('PER_PAGE')

proxies = {
    "http": "socks5h://127.0.0.1:10808",
    "https": "socks5h://127.0.0.1:10808",
}

def get_all_starred_repos():
    """获取所有已 star 的仓库（全量获取）"""
    url = f'https://api.github.com/users/{GITHUB_USERNAME}/starred'
    headers = {
        'Authorization': f'token {GITHUB_TOKEN}',
        'Accept': 'application/vnd.github.v3.star+json'
    }
    params = {
        'per_page': PER_PAGE,
        'page': 1
    }
    
    starred_repos = []
    while True:
        response = requests.get(url, headers=headers, params=params, proxies=proxies)
        response.raise_for_status()
        data = response.json()
        if not data:
            break
            
        for repo in data:
            print('repo:', repo)
            starred_repos.append({
                'id': repo['repo']['id'],
                'name': repo['repo']['full_name'],
                'url': repo['repo']['html_url'],
                'starred_at': repo['starred_at'],
                'description': repo['repo']['description'],
                'last_updated': datetime.now().isoformat()
            })
        
        # 检查是否有下一页
        if 'next' in response.links:
            url = response.links['next']['url']
            params = {}  # 下一页 URL 已经包含所有参数
        else:
            break
    
    return starred_repos

def get_new_starred_repos(existing_ids):
    """获取新增的 star 仓库（增量获取）"""
    url = f'https://api.github.com/users/{GITHUB_USERNAME}/starred'
    headers = {
        'Authorization': f'token {GITHUB_TOKEN}',
        'Accept': 'application/vnd.github.v3.star+json'
    }
    params = {
        'per_page': PER_PAGE,
        'page': 1,
        'sort': 'created',  # 按创建时间排序，方便获取最新的
        'direction': 'desc'  # 降序排列，最新的在前面
    }
    
    new_repos = []
    while True:
        response = requests.get(url, headers=headers, params=params, proxies=proxies)
        response.raise_for_status()
        data = response.json()
        if not data:
            break
            
        for repo in data:
            repo_id = repo['repo']['id']
            if repo_id in existing_ids:
                # 如果遇到已存在的 ID，说明后面的都是已处理的
                return new_repos
                
            new_repos.append({
                'id': repo['repo']['id'],
                'name': repo['repo']['full_name'],
                'url': repo['repo']['html_url'],
                'starred_at': repo['starred_at'],
                'description': repo['repo']['description'],
                'last_updated': datetime.now().isoformat()
            })
        
        # 检查是否有下一页
        if 'next' in response.links:
            url = response.links['next']['url']
            params = {}  # 下一页 URL 已经包含所有参数
        else:
            break
    
    return new_repos

def load_existing_data():
    """加载已存储的数据"""
    if os.path.exists(STAR_FILE):
        with open(STAR_FILE, 'r') as f:
            return json.load(f)
    return []

def save_data(data):
    """保存数据到文件"""
    with open(STAR_FILE, 'w') as f:
        json.dump(data, f, indent=2)

def main():
    # 加载已有数据
    existing_data = load_existing_data()
    existing_ids = {repo['id'] for repo in existing_data}
    
    if not existing_data:
        print("首次运行，进行全量获取...")
        starred_repos = get_all_starred_repos()
        save_data(starred_repos)
        print(f"获取并保存了 {len(starred_repos)} 个 star 仓库")
    else:
        print("增量获取新增 star 仓库...")
        new_repos = get_new_starred_repos(existing_ids)
        
        if new_repos:
            updated_data = new_repos + existing_data  # 新的放在前面
            save_data(updated_data)
            print(f"新增 {len(new_repos)} 个 star 仓库")
            
            # 这里可以添加后续处理逻辑，如下载、记录等
            for repo in new_repos:
                print(f"新增仓库: {repo['name']} - {repo['url']}")
                # process_repo(repo)  # 你的后续处理函数
        else:
            print("没有新增的 star 仓库")

if __name__ == '__main__':
    main()
